#include "Hw4RandomNumbers.h"
#include <stdio.h>
#include <time.h>

#include <math.h>
#ifndef M_PI
	#define M_PI 3.14159265358979323846
#endif

//#define RANDOM_DEBUG

int RandomCounter = 0;

/*..............................................................*/

void InitializeRandomNumberGenerator( unsigned int seed )
{
	srand( seed );
}

/*..............................................................*/

void InitializeRandomNumberGeneratorWithoutSeed()
{
	srand( (unsigned int)time( NULL ) );
}

/*..............................................................*/

double RandomUniform()
{
	double RandRes = (double)rand() / (double)RAND_MAX;
	#ifdef RANDOM_DEBUG
		RandomCounter++;
		printf("RANDOM: RandomUniform Called. Random result: %.3g. Random counter is: %d\n", RandRes, RandomCounter);
	#endif
	return RandRes;
}

/*..............................................................*/

static double drand()   /* uniform distribution, (0..1] */
{
  return (rand()+1.0)/(RAND_MAX+1.0);
}

double RandomGaussian()  /* normal distribution, centered on 0, std dev 1 */
{
	double RandRes = sqrt(-2*log(drand())) * cos(2*M_PI*drand());
	#ifdef RANDOM_DEBUG
		RandomCounter++;
		printf("RANDOM: RandomGaussian Called. Random result: %.3g. Random counter is: %d\n", RandRes, RandomCounter);
	#endif
	return RandRes;
}

/*..............................................................*/
int RandomInteger( int Min, int Max )
{
	int NumOfOptions = (Max-Min+1);
	int Offset = rand() % NumOfOptions;
	int RandRes = Min + Offset;
	#ifdef RANDOM_DEBUG
		RandomCounter++;
		printf("RANDOM: RandomInteger Called. Random result: %d. Random counter is: %d\n", RandRes, RandomCounter);
	#endif
	return RandRes;
}